package com.kexio.common.enums;

/**
 * 业务操作类型枚举
 * 用于操作日志记录
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum BusinessType {
    
    /**
     * 其它
     */
    OTHER("OTHER", "其它"),
    
    /**
     * 新增
     */
    INSERT("INSERT", "新增"),
    
    /**
     * 修改
     */
    UPDATE("UPDATE", "修改"),
    
    /**
     * 删除
     */
    DELETE("DELETE", "删除"),
    
    /**
     * 批量删除
     */
    BATCH_DELETE("BATCH_DELETE", "批量删除"),
    
    /**
     * 查询
     */
    SELECT("SELECT", "查询"),
    
    /**
     * 导入
     */
    IMPORT("IMPORT", "导入"),
    
    /**
     * 导出
     */
    EXPORT("EXPORT", "导出"),
    
    /**
     * 登录
     */
    LOGIN("LOGIN", "登录"),
    
    /**
     * 登出
     */
    LOGOUT("LOGOUT", "登出"),
    
    /**
     * 授权
     */
    GRANT("GRANT", "授权"),
    
    /**
     * 重置密码
     */
    RESET_PASSWORD("RESET_PASSWORD", "重置密码"),
    
    /**
     * 上传文件
     */
    UPLOAD("UPLOAD", "上传文件"),
    
    /**
     * 下载文件
     */
    DOWNLOAD("DOWNLOAD", "下载文件"),
    
    /**
     * 清理缓存
     */
    CLEAR_CACHE("CLEAR_CACHE", "清理缓存"),
    
    /**
     * 同步数据
     */
    SYNC_DATA("SYNC_DATA", "同步数据"),
    
    /**
     * 备份数据
     */
    BACKUP("BACKUP", "备份数据"),
    
    /**
     * 恢复数据
     */
    RESTORE("RESTORE", "恢复数据");
    
    private final String code;
    private final String desc;
    
    BusinessType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getDesc() {
        return desc;
    }
    
    /**
     * 根据code获取枚举
     */
    public static BusinessType fromCode(String code) {
        if (code == null) {
            return null;
        }
        for (BusinessType type : values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }
        return OTHER;
    }
    
    /**
     * 是否为写操作
     */
    public boolean isWriteOperation() {
        return this == INSERT || this == UPDATE || this == DELETE || this == BATCH_DELETE
                || this == IMPORT || this == GRANT || this == RESET_PASSWORD
                || this == UPLOAD || this == CLEAR_CACHE || this == SYNC_DATA
                || this == BACKUP || this == RESTORE;
    }
    
    /**
     * 是否为读操作
     */
    public boolean isReadOperation() {
        return this == SELECT || this == EXPORT || this == DOWNLOAD;
    }
    
    @Override
    public String toString() {
        return "BusinessType{" +
                "code='" + code + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}
